package com.ssy.lingxi.purchase.controller.tender;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.feign.ICommodityFeign;
import com.ssy.lingxi.product.api.model.response.commodity.CommodityBaseResponse;
import com.ssy.lingxi.purchase.api.dto.response.tender.SubmitTenderMaterielResponse;
import com.ssy.lingxi.purchase.entity.tender.SubmitTenderMateriel;
import com.ssy.lingxi.purchase.service.tender.ISubmitTenderMaterielService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu: 招投标-投标物料管理
 * 投标物料管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/3/1
 */
@RestController
@RequestMapping("/purchase/submitTenderMateriel")
public class SubmitTenderMaterielController extends BaseController {

    @Resource
    private ICommodityFeign commodityFeign;

    @Resource
    private ISubmitTenderMaterielService submitTenderMaterielService;

    /**
     * 根据投标id查找物料
     * @param submitTenderId 投标id
     */
    @RequestMapping(value = "/getSubmitTenderMaterielList", method = RequestMethod.GET)
    public Wrapper<PageData<SubmitTenderMaterielResponse>> getSubmitTenderMaterielList(PageVO pageVO, @RequestParam Long submitTenderId){
        Page<SubmitTenderMateriel> result = submitTenderMaterielService.getSubmitTenderMaterielList(pageVO, submitTenderId, true);
        if(result.getContent().size() > 0){
            List<SubmitTenderMaterielResponse> resultList = result.getContent().stream().map(submitTenderMateriel -> {
                SubmitTenderMaterielResponse submitTenderMaterielResponse = BeanUtil.copyProperties(submitTenderMateriel, SubmitTenderMaterielResponse.class);
                Long commoditySkuId = submitTenderMateriel.getCommoditySkuId();
                if(commoditySkuId != null && commoditySkuId > 0){
                    CommodityBaseResponse commodity = commodityFeign.getCommodityBySkuId(commoditySkuId);
                    if(commodity != null){
                        submitTenderMaterielResponse.setCommodityId(commodity.getId());
                        submitTenderMaterielResponse.setCommodityName(commodity.getName());
                        submitTenderMaterielResponse.setCommodityBrand(commodity.getBrand());
                        submitTenderMaterielResponse.setCommodityAttribute(commodity.getCommodityAttribute());
                        submitTenderMaterielResponse.setCommodityCategory(commodity.getCustomerCategory());
                        submitTenderMaterielResponse.setCommodityCode(commodity.getCode());
                    }
                }
                return submitTenderMaterielResponse;
            }).collect(Collectors.toList());
            return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
        }else{
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }
    }
}
